package zhuyun.code.swipe

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Scroller
import kotlin.math.abs

/**
 * @author: Administrator
 * @date: 2022/1/6
 */
class SwipeLayout(private val mContext: Context, private val attrs: AttributeSet?, private val defStyleAttr:Int):FrameLayout(mContext,attrs,defStyleAttr) {
    constructor(mContext: Context,attrs: AttributeSet?):this(mContext,attrs,0)
    constructor(mContext: Context):this(mContext,null)

    private val mScroller = Scroller(mContext)
    private var swipeWidth = 0
    private val sileInt = ViewConfiguration.get(mContext).scaledTouchSlop

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val child = getChildAt(0)
        val layoutParams = child.layoutParams
        val childWidthMeasureSpec: Int = MeasureSpec.makeMeasureSpec(
            Math.max(0, MeasureSpec.getSize(widthMeasureSpec)),
            MeasureSpec.UNSPECIFIED)
        val childHeightMeasureSpec = ViewGroup.getChildMeasureSpec(
            heightMeasureSpec,0,
            layoutParams.height)
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
    }
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if(changed){
            //
            val rootChild = getChildAt(0) as LinearLayout
            rootChild.measuredWidth
            val swipeView = rootChild.getChildAt(1)
            swipeWidth = swipeView.measuredWidth
        }

    }

    override fun computeScroll() {
        if(mScroller.computeScrollOffset()){
            this.scrollTo(mScroller.currX,mScroller.currY)
            postInvalidate()
        }
    }

    private var currentX = 0
    private var currentY = 0
    private var lastX = 0
    private var lastY = 0

    override fun onTouchEvent(event: MotionEvent): Boolean {
        currentX = event.x.toInt()
        currentY = event.y.toInt()
        when(event.action){
            MotionEvent.ACTION_DOWN->{

            }
            MotionEvent.ACTION_MOVE->{
                val distance = lastX - currentX
                if(scrollX + distance > swipeWidth){//左滑
                    this.scrollTo(swipeWidth,0)
                    return true
                } else if(scrollX + distance < 0){
                    this.scrollTo(0,0)
                    return true
                }
                this.scrollBy(distance,0)
            }
            else->{
                if(this.scrollX > swipeWidth/2){
                    swipeOpen(true, smoothScroll = true)
                } else {
                    swipeClose(true, smoothScroll = true)
                }
            }

        }
        lastX = currentX
        lastY = currentY
        return true
    }
    private var swipeStatus = false
    fun getSwipeStatus():Boolean{
        return swipeStatus
    }

    private var mSwipeListener:SwipeListener? = null

    fun setSwipeListener(swipeListener: SwipeListener){
        this.mSwipeListener = swipeListener
    }

    fun swipeOpen(isFromActionUp:Boolean,smoothScroll: Boolean){
        mSwipeListener?.onOpen(isFromActionUp,smoothScroll)
        if(smoothScroll){
            mScroller.startScroll(this.scrollX,0,swipeWidth-this.scrollX,0,abs(swipeWidth-this.scrollX))
            postInvalidate()
        } else {
            scrollTo(swipeWidth,0)
        }
        swipeStatus = true
    }

    fun swipeClose(isFromActionUp: Boolean,smoothScroll:Boolean){
        mSwipeListener?.onClose(isFromActionUp,smoothScroll)
        if(smoothScroll){
            mScroller.startScroll(this.scrollX,0,-this.scrollX,0,abs(-this.scrollX))
            postInvalidate()
        } else {
            scrollTo(0,0)
        }
        swipeStatus = false
    }

    interface SwipeListener{
        fun onOpen(isFromActionUp: Boolean,smoothScroll: Boolean)
        fun onClose(isFromActionUp: Boolean,smoothScroll: Boolean)
    }


}